JavaScript Unary Operators

Visualizing `delete`, `void`, `typeof`, `+`, `-`, `~`, `!`, `await`.

`delete`, `void`, `typeof`

**`delete`** removes a property from an object. **`void`** evaluates an expression and returns `undefined`. **`typeof`** returns a string indicating the type of its operand.

const obj = { a: 1 }; delete obj.a; // obj is {}
void(0); // undefined
typeof "hello"; // "string"

`delete` Operator

Initial Object:

Result of `delete obj.b`:

`void` and `typeof`

Expression `void(10 + 5)`:

Expression `typeof null`:


`+`, `-`, `~`, `!`

The unary plus (`+`) attempts to convert its operand to a number. The unary minus (`-`) negates its operand. The bitwise NOT (`~`) inverts all bits. The logical NOT (`!`) inverts the boolean value of its operand.

+"5";    // 5 (number)
-"5";    // -5 (number)
~5;      // -6
!true;   // false

`+` and `-`

Operand: `let str = "123"`

Result of `+str`:

Result of `-str`:

`~` and `!`

Operand: `let num = 5`

Result of `~num`:

Operand: `let bool = true`

Result of `!bool`:


`await` Operator

The **`await`** operator can only be used inside an `async` function. It pauses the execution of the `async` function until the `Promise` it is waiting for settles (either fulfills or rejects) and then resumes the `async` function's execution.

async function fetchData() {
  const data = await fetch('/api/data');
  return data.json();
}

Simulating an asynchronous operation that takes 2 seconds.

`await` Demo

Status: Ready

Result of `await` expression: